package event;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import message.MissileNewMsg;
import message.TankMoveMsg;
import user.source.TankClient;

/**
 * 
 * @author
 *
 */
public class Tank {
	/**
	 * 一辆坦克就是一个用户，将ID号保存在坦克类中
	 */
	public int id;

	public static final int X_SPEED = 5;
	public static final int Y_SPEED = 5;

	public static final int WIDTH = 30;
	public static final int HEIGHT = 30;

	private boolean live = true;

	TankClient tc;

	public boolean good;

	/**
	 * 坦克位置
	 */
	public int x;
	public int y;

	private boolean bL = false, bU = false, bR = false, bD = false;

	/**
	 * 坦克初始化时方向是停滞状态
	 */
	public Direction dir = Direction.STOP;
	public Direction barrel = Direction.D;

	public Tank(int x, int y, boolean good) {
		this.x = x;
		this.y = y;
		this.good = good;
	}

	public Tank(int x, int y, boolean good, Direction dir, TankClient tc) {
		this(x, y, good);
		this.dir = dir;
		this.tc = tc;
	}

	/**
	 * 让坦克画出自己
	 * 
	 * @param g
	 */
	public void draw(Graphics g) {
		// 如果坦克死亡，就不再画出坦克
		if (!live) {
			if (!good) {
				tc.tanks.remove(this);
			}
			return;
		}

		Color c = g.getColor();
		if (good) {
			g.setColor(Color.RED);
		} else {
			g.setColor(Color.CYAN);
		}
		// 坦克位置坐标为（x，y），宽高为30
		g.fillRect(x, y, WIDTH, HEIGHT);
		g.drawString("ID:" + id, x, y - 10);
		g.setColor(c);

		switch (barrel) {
		case L:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2);
			break;
		case LU:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y);
			break;
		case U:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y);
			break;
		case RU:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y);
			break;
		case R:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2);
			break;
		case RD:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT);
			break;
		case D:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
			break;
		case LD:
			g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT);
			break;
		default:
		}

		// 让坦克行动的方法
		move();
	}

	/**
	 * 实现坦克行走的方法
	 */
	void move() {
		switch (dir) {
		case L:
			x -= X_SPEED;
			break;
		case LU:
			x -= X_SPEED / 1.414;
			y -= Y_SPEED / 1.414;
			break;
		case U:
			y -= Y_SPEED;
			break;
		case RU:
			x += X_SPEED / 1.414;
			y -= Y_SPEED / 1.414;
			break;
		case R:
			x += X_SPEED;
			break;
		case RD:
			x += X_SPEED / 1.414;
			y += Y_SPEED / 1.414;
			break;
		case D:
			y += Y_SPEED;
			break;
		case LD:
			x -= X_SPEED / 1.414;
			y += Y_SPEED / 1.414;
			break;
		case STOP:
			break;
		default:
		}

		if (this.dir != Direction.STOP) {
			// 如果键盘停止输入方向，则炮管朝向最后输入的方向
			this.barrel = this.dir;
		}

		// 设置坦克的活动范围，建立空气墙
		if (x < 0) {
			x = 0;
		}
		if (y < Tank.WIDTH) {
			y = 30;
		}
		if (x + Tank.WIDTH > TankClient.GAMME_WIDTH) {
			x = TankClient.GAMME_WIDTH - Tank.WIDTH;
		}
		if (y + Tank.HEIGHT > TankClient.GAMME_HEIGHT) {
			y = TankClient.GAMME_HEIGHT - Tank.HEIGHT;
		}

	}

	/**
	 * 坦克开火的方法
	 * 
	 * @return
	 */
	public Missile fire() {
		if (!live) {
			return null;
		}

		int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
		int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
		// 根据炮筒的方向发射炮弹
		Missile missile = new Missile(id, x, y, this.good, barrel, this.tc);
		// 添加炮弹
		tc.missiles.add(missile);

		MissileNewMsg msg = new MissileNewMsg(missile);
		tc.net.send(msg);

		return missile;
	}

	/**
	 * 按键按下响应方法
	 * 
	 * @param e
	 */
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_A:
			bL = true;
			break;
		case KeyEvent.VK_W:
			bU = true;
			break;
		case KeyEvent.VK_D:
			bR = true;
			break;
		case KeyEvent.VK_S:
			bD = true;
			break;
		default:
		}
		// 按下一个键后，方向才会改变
		locateDirection();
	}

	/**
	 * 按键抬起响应方法
	 * 
	 * @param e
	 */
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_J:
			fire();
			break;
		case KeyEvent.VK_A:
			bL = false;
			break;
		case KeyEvent.VK_W:
			bU = false;
			break;
		case KeyEvent.VK_D:
			bR = false;
			break;
		case KeyEvent.VK_S:
			bD = false;
			break;
		default:
		}
		locateDirection();
	}

	/**
	 * 根据按键来判断行走方向的方法
	 */
	void locateDirection() {
		Direction oldDirection = this.dir;

		if (bL && !bU && !bR && !bD) {
			dir = Direction.L;
		} else if (bL && bU && !bR && !bD) {
			dir = Direction.LU;
		} else if (!bL && bU && !bR && !bD) {
			dir = Direction.U;
		} else if (!bL && bU && bR && !bD) {
			dir = Direction.RU;
		} else if (!bL && !bU && bR && !bD) {
			dir = Direction.R;
		} else if (!bL && !bU && bR && bD) {
			dir = Direction.RD;
		} else if (!bL && !bU && !bR && bD) {
			dir = Direction.D;
		} else if (bL && !bU && !bR && bD) {
			dir = Direction.LD;
		} else if (!bL && !bU && !bR && !bD) {
			dir = Direction.STOP;
		}

		if (dir != oldDirection) {
			TankMoveMsg msg = new TankMoveMsg(id, x, y, dir, barrel);
			tc.net.send(msg);
		}

	}

	public Rectangle getRect() {
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

}